/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.huikecloud.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.huikecloud.entity.Jmeter;
import org.springblade.modules.huikecloud.service.IJmeterService;
import org.springblade.modules.huikecloud.utils.ScriptUtil;
import org.springblade.modules.huikecloud.vo.JmeterVO;
import org.springblade.modules.huikecloud.wrapper.JmeterWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 *  控制器
 *
 * @author Blade
 * @since 2021-08-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("pts/jmeter")
@Api(value = "", tags = "接口")
public class JmeterController extends BladeController {

	private IJmeterService jmeterService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入jmeter")
	public R<JmeterVO> detail(Jmeter jmeter) {
		Jmeter detail = jmeterService.getOne(Condition.getQueryWrapper(jmeter));
		return R.data(JmeterWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入jmeter")
	public R<IPage<Jmeter>> list(Jmeter jmeter, Query query) {
//		jmeter.setParentId(0L);
		IPage<Jmeter> pages = jmeterService.page(Condition.getPage(query), Condition.getQueryWrapper(jmeter));
		return R.data(pages);
	}


	/**
	 * 自定义分页 
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入jmeter")
	public R<IPage<JmeterVO>> page(JmeterVO jmeter, Query query) {
		IPage<JmeterVO> pages = jmeterService.selectJmeterPage(Condition.getPage(query), jmeter);
		return R.data(pages);
	}

	/**
	 * 新增 
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入jmeter")
	public R save(@Valid @RequestBody Jmeter jmeter) {
		return R.status(jmeterService.save(jmeter));
	}

	/**
	 * 修改 
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入jmeter")
	public R update(@Valid @RequestBody Jmeter jmeter) {
		return R.status(jmeterService.updateById(jmeter));
	}

	/**
	 * 新增或修改 
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入jmeter")
	public R submit(@Valid @RequestBody Jmeter jmeter) {
		return R.status(jmeterService.saveOrUpdate(jmeter));
	}

	
	/**
	 * 删除 
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> longs = Func.toLongList(ids);
		for (Long id:longs) {
			jmeterService.deleteFile(id);
		}
		return R.status(jmeterService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 上传压测脚本
	 */
	@PostMapping("/upload_jmx")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "上传压测脚本", notes = "传入脚本")
	public R upload_jmx(@RequestParam MultipartFile file) {
		return jmeterService.upload_jmx(file);
	}


	/**
	 * 执行压测脚本
	 */
	@PostMapping("/run")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "执行压测脚本", notes = "传入jmeter")
	public R run( @RequestBody Jmeter jmeter) {
		return jmeterService.run(jmeter);
	}

}
